package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.ISearchResultFragmentModel;
import com.wgts.wgts.mvp.presenter.ISearchResultFragmentPresenter;
import com.wgts.wgts.mvp.view.ISearchResultFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.SearchShopListRequest;
import com.wgts.wgts.netmodle.req.SearchStoreListRequset;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.netmodle.resp.SearchStoreResultEntity;

/*
 *
 *
 *   data:2018/9/2715:20
 *
 */public class SearchResultFragmentPresenter implements ISearchResultFragmentPresenter {
     private ISearchResultFragmentModel model;
     private ISearchResultFragmentView view;
     private int pageSize = 20;
     private int shopPageIndex = 1;
     private int StorePageIndex = 1;

    public SearchResultFragmentPresenter(ISearchResultFragmentModel model, ISearchResultFragmentView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void getShopList(String name) {
        shopPageIndex = 1;
        SearchShopListRequest shopListRequest = new SearchShopListRequest();
        shopListRequest.name = name;
        shopListRequest.pageIndex = shopPageIndex;
        shopListRequest.pageSize = pageSize;
        model.getShopList(shopListRequest, new HttpResultSubscriber<SearchShopResultEntity>() {
            @Override
            public void onSuccess(SearchShopResultEntity resultEntity) {
                if (resultEntity.list.size() < pageSize){
                    view.getShopList(resultEntity, true);
                }else {
                    view.getShopList(resultEntity, false);
                    shopPageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void getShopMore(String name) {
        SearchShopListRequest shopListRequest = new SearchShopListRequest();
        shopListRequest.name = name;
        shopListRequest.pageIndex = shopPageIndex;
        shopListRequest.pageSize = pageSize;
        model.getShopList(shopListRequest, new HttpResultSubscriber<SearchShopResultEntity>() {
            @Override
            public void onSuccess(SearchShopResultEntity resultEntity) {
                if (resultEntity.list.size() < pageSize){
                    view.loadMoreShop(resultEntity, true);
                }else {
                    view.loadMoreShop(resultEntity, false);
                    shopPageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void getStoreList(String name) {
        StorePageIndex = 1;
        SearchStoreListRequset storeListRequset = new SearchStoreListRequset();
        storeListRequset.pageIndex = StorePageIndex;
        storeListRequset.name = name;
        storeListRequset.pageSize = pageSize;
        model.getStoreList(storeListRequset, new HttpResultSubscriber<SearchStoreResultEntity>() {
            @Override
            public void onSuccess(SearchStoreResultEntity searchStoreResultEntity) {
                if (searchStoreResultEntity.list.size() < pageSize){
                    view.getStoreList(searchStoreResultEntity, true);
                }else {
                    view.getStoreList(searchStoreResultEntity, false);
                    StorePageIndex ++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void getStoreMore(String name) {
        SearchStoreListRequset storeListRequset = new SearchStoreListRequset();
        storeListRequset.pageIndex = StorePageIndex;
        storeListRequset.name = name;
        storeListRequset.pageSize = pageSize;
        model.getStoreList(storeListRequset, new HttpResultSubscriber<SearchStoreResultEntity>() {
            @Override
            public void onSuccess(SearchStoreResultEntity searchStoreResultEntity) {
                if (searchStoreResultEntity.list.size() < pageSize){
                    view.loadMoreStore(searchStoreResultEntity, true);
                }else {
                    view.loadMoreStore(searchStoreResultEntity, false);
                    StorePageIndex ++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
